package com.ruoyi.web.controller.frontierCommand;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.frontierCommand.vo.TPatrolMissionVo;
import com.ruoyi.system.frontierCommand.bo.TPatrolMissionQueryBo;
import com.ruoyi.system.frontierCommand.bo.TPatrolMissionAddBo;
import com.ruoyi.system.frontierCommand.bo.TPatrolMissionEditBo;
import com.ruoyi.system.frontierCommand.service.ITPatrolMissionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 巡护任务Controller
 *
 * @author ruoyi
 * @date 2021-05-19
 */
@Api(value = "巡护任务控制器", tags = {"巡护任务管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/frontierCommand/mission")
public class TPatrolMissionController extends BaseController {

    private final ITPatrolMissionService iTPatrolMissionService;

    /**
     * 查询巡护任务列表
     */
    @ApiOperation("查询巡护任务列表")
//    @PreAuthorize("@ss.hasPermi('frontierCommand:mission:list')")
    @GetMapping("/list")
    public TableDataInfo<TPatrolMissionVo> list(TPatrolMissionQueryBo bo) {
        startPage();
        List<TPatrolMissionVo> list = iTPatrolMissionService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出巡护任务列表
     */
    @ApiOperation("导出巡护任务列表")
    @PreAuthorize("@ss.hasPermi('frontierCommand:mission:export')")
    @Log(title = "巡护任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<TPatrolMissionVo> export(TPatrolMissionQueryBo bo) {
        startPage();
        List<TPatrolMissionVo> list = iTPatrolMissionService.queryList(bo);
        ExcelUtil<TPatrolMissionVo> util = new ExcelUtil<TPatrolMissionVo>(TPatrolMissionVo.class);
        return util.exportExcel(list, "巡护任务");
    }

    /**
     * 获取巡护任务详细信息
     */
    @ApiOperation("获取巡护任务详细信息")
    @PreAuthorize("@ss.hasPermi('frontierCommand:mission:query')")
    @GetMapping("/{id}")
    public AjaxResult<TPatrolMissionVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iTPatrolMissionService.queryById(id));
    }

    /**
     * 新增巡护任务
     */
    @ApiOperation("新增巡护任务")
    @PreAuthorize("@ss.hasPermi('frontierCommand:mission:add')")
    @Log(title = "巡护任务", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody TPatrolMissionAddBo bo) {
        if ("1".equals(iTPatrolMissionService.checkNameUnique(bo.getMission(), 0))) {
            return AjaxResult.error("新增巡护任务：'" + bo.getMission() + "'失败，已存在");
        }
        return toAjax(iTPatrolMissionService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改巡护任务
     */
    @ApiOperation("修改巡护任务")
    @PreAuthorize("@ss.hasPermi('frontierCommand:mission:edit')")
    @Log(title = "巡护任务", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody TPatrolMissionEditBo bo) {
        if ("1".equals(iTPatrolMissionService.checkNameUnique(bo.getMission(), bo.getId().intValue()))) {
            return AjaxResult.error("修改巡护任务：'" + bo.getMission() + "'失败，已存在");
        }
        return toAjax(iTPatrolMissionService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除巡护任务
     */
    @ApiOperation("删除巡护任务")
    @PreAuthorize("@ss.hasPermi('frontierCommand:mission:remove')")
    @Log(title = "巡护任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iTPatrolMissionService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
